package uppacket

import (
	"gitee.com/yanwc/ebike-xiaoan-device/device"
	"gitee.com/yanwc/ebike-xiaoan-device/internal/codec"
	"gitee.com/yanwc/ebike-xiaoan-device/internal/field"
	"gitee.com/yanwc/ebike-xiaoan-device/internal/frame"
	"gitee.com/yanwc/net/utils"

	"github.com/panjf2000/gnet"
)

type EventNotifyPacket struct{}

var _ IUpPacket = (*EventNotifyPacket)(nil)

func (hbp *EventNotifyPacket) Do(frame frame.IFrame, c gnet.Conn) ([]byte, gnet.Action, error) {
	var warn field.AlarmTypeRaw
	utils.LoadPacket(frame.Content(), warn[:])
	if d, ok := device.SessionDevices.GetWithConn(c); ok {
		go d.EventNotify(warn.Type())
	}

	f := &codec.Frame{
		Header: &codec.Header{
			CmdWord: frame.GetCmdWord(),
			SN:      frame.GetSN(),
		},
	}
	return f.WrapPacket(nil), gnet.None, nil
}
